var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    detail:{},
    full:0,
    half:0,
    empty:0,
    commentList:[],
    latitude: 0,
    longitude: 0,
    shopid:"",
    total_page:0,
    now_page:1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      shopid:options.id
    })
    this.getaddress();
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.onLoad();
  },
  // 获取当前位置
  getaddress: function () {
    var that = this;

    // 获取当前地理位置
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        var latitude = res.latitude
        var longitude = res.longitude
        that.setData({
          latitude: latitude,
          longitude: longitude
        })
        that.getdetail(longitude, latitude);
        that.getpjlist()
      }
    })
  },
  // 获取门店信息
  getdetail: function (longitude,latitude){
    var that = this;
    var shopid = wx.getStorageSync('shop_id');
    var param = {
      url:'api/shopr/shopInfo',
      data:{
        shop_id:this.data.shopid,
        longitude: longitude,
        latitude: latitude
      },
      success:function(e){
        console.log(e)
         
        if (e.data.data.avg_score){
          var score = e.data.data.avg_score;
          var full = Math.floor(score / 2);
          var half;
          var empty;
          if (parseInt(score / 2) == score / 2) {
            half = 0
          } else {
            half = 1
          }
          empty = 5 - full - half;
        }else{
          var empty = 5;
          half=0;
          full = 0;
        }
          
       
        that.setData({
          detail:e.data.data,
          full:full,
          empty:empty,
          half:half
        })
      }
    };
    app.post(param);
  },
  previewBanner(e){
    let index = e.currentTarget.dataset.index;
    let mentou = this.data.detail.mentou;
    wx.previewImage({
      current: mentou[index], // 当前显示图片的http链接
      urls: mentou // 需要预览的图片http链接列表
    })
  },
  getpjlist(now_page = 1){
    var that = this;
    var param = {
      url:"api/index/getEvaluations",
      data:{
        shop_id:this.data.shopid,
        now_page:now_page
      },
      success:function(e){
        // var scores=[];
        // var full;
        // var empty;
        // empty = 5 - full - half;
        var list = e.data.data.list;
        // for(var i=0;i<list.length;i++){
        //   var score = list[i].score;
        //   full = score / 2;
        //   empty=5-full;
        //   list[i].full=full;
        //   list[i].empty = empty;
        // }
        that.setData({
          total_page:e.data.data.total_page,
          commentList: that.data.commentList.concat(list) 
        })
        console.log(list)
      }
    };
    app.post(param);
  },
    /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let now_page = this.data.now_page;
    console.log(now_page,this.data.total_page)
    if(now_page < this.data.total_page){
      now_page++;
      this.setData({
        now_page:now_page
      })
      this.getpjlist(now_page)
    }
},
  daohang: function () {
    var detail = this.data.detail;
    wx.openLocation({
      latitude: detail.s_latitude,
      longitude: detail.s_longitude,
      scale: 18,
      name: detail.shop_name,
      address: detail.address
    })
  }
})